$ontext
===================================================================================
Name: runVSPD.gms
Function: This controls the whole process in standalone mode
Developed by: Ramu Naidoo
Last modified: 01 December 2010
===================================================================================
$offtext

$call cls
$onecho > con
************************************************************
******************EXECUTING vSPD v1.0***********************
************************************************************
$offecho

*==================================
*Definitions and general settings
*==================================

$onecho > vSPDpaths.inc

*Specify the run name
$setglobal       runName                "Test"

$setglobal       ProgramPath     "%system.fp%"
$setglobal       InputPath       "%system.fp%..\Input\"
$setglobal       OvrdPath        "%system.fp%..\Overrides\"
$setglobal       OutputPath      "%system.fp%..\Output\"
$setglobal       InputFilePath   "%system.fp%\Input\"

*Set the Mode (0 = Developer, 1 = Runtime)
$setglobal       Mode                  1

*Change this to temp when running in standalone mode
$setglobal       VSPDInputData          temp

*Include the override file and format (1 = gdx, 2 = xls)
$setglobal       VSPDInputOvrdData      "VSPD_InputOvrdData"
$setglobal       VSPDInputOvrdFormat    2


*xls file name in the input directory that contains the list of files to execute
*as part of this run
$setglobal       VSPDInputFileName      "VSPDInputFileName"

$offecho

*Include settings file
$include vSPDpaths.inc
$include vSPDsettings.inc

*Temp file definition
File
temp;

*Call setup file
put_utility temp 'exec' / 'gams runVSPDSetup';

*Now execute in standalone mode

SETS
*Input file name
i_FileName(*)       'Filenames'
;

* Import input data from Excel data file via GDX.
* Write arguments for the GDX call to gdxVSPDInputData.ins:
$ONECHO > gdxInputFileName.ins
* Parameters and sets
         set = i_FileName                rng = i_FileName                  rdim = 1
$OFFECHO

* Call the GDX routine and load the input data:
$CALL 'GDXXRW "%InputPath%%VSPDInputFileName%.xls" o=InputFileName.gdx "@gdxInputFileName.ins"'
$GDXIN InputFileName.gdx

$LOAD i_FileName
*Close the gdx
$GDXIN

*Loop over all the specified files and run the model
loop(i_FileName,

*Rename the input file name for the model
    put_utility temp 'shell' / 'copy %InputPath%'i_FileName.tl:0'.gdx %InputPath%%VSPDInputData%.gdx';

*Run the model
    put_utility temp 'exec' / 'gams runVSPDSolve';

*Rename the output gdx files from the model run
    put_utility temp 'shell' / 'rename %OutputPath%%runName%\%VSPDInputData%_SystemOutput.gdx 'i_FileName.tl:0'_SystemOutput.gdx';
    put_utility temp 'shell' / 'rename %OutputPath%%runName%\%VSPDInputData%_OfferOutput.gdx 'i_FileName.tl:0'_OfferOutput.gdx';
    put_utility temp 'shell' / 'rename %OutputPath%%runName%\%VSPDInputData%_TraderOutput.gdx 'i_FileName.tl:0'_TraderOutput.gdx';

    if (%TradePeriodReports% = 1,
        put_utility temp 'shell' / 'rename %OutputPath%%runName%\%VSPDInputData%_BusOutput_TP.gdx 'i_FileName.tl:0'_BusOutput_TP.gdx';
        put_utility temp 'shell' / 'rename %OutputPath%%runName%\%VSPDInputData%_BranchOutput_TP.gdx 'i_FileName.tl:0'_BranchOutput_TP.gdx';
        put_utility temp 'shell' / 'rename %OutputPath%%runName%\%VSPDInputData%_NodeOutput_TP.gdx 'i_FileName.tl:0'_NodeOutput_TP.gdx';
        put_utility temp 'shell' / 'rename %OutputPath%%runName%\%VSPDInputData%_OfferOutput_TP.gdx 'i_FileName.tl:0'_OfferOutput_TP.gdx';
        put_utility temp 'shell' / 'rename %OutputPath%%runName%\%VSPDInputData%_BidOutput_TP.gdx 'i_FileName.tl:0'_BidOutput_TP.gdx';
        put_utility temp 'shell' / 'rename %OutputPath%%runName%\%VSPDInputData%_ReserveOutput_TP.gdx 'i_FileName.tl:0'_ReserveOutput_TP.gdx';
        put_utility temp 'shell' / 'rename %OutputPath%%runName%\%VSPDInputData%_BrConstraintOutput_TP.gdx 'i_FileName.tl:0'_BrConstraintOutput_TP.gdx';
        put_utility temp 'shell' / 'rename %OutputPath%%runName%\%VSPDInputData%_MNodeConstraintOutput_TP.gdx 'i_FileName.tl:0'_MNodeConstraintOutput_TP.gdx';
    );

*Remove the temporary gdx file from the input directory
    put_utility temp 'shell' / 'del %InputPath%%VSPDInputData%.gdx';
);

*==================================
*Report Results
*==================================

*Setup the report templates
put_utility temp 'exec' / 'gams VSPDReportSetup';

*Loop over all the specified files and update the reports
loop(i_FileName,

*Rename the output gdx files from the model run
    put_utility temp 'shell' / 'rename %OutputPath%%runName%\'i_FileName.tl:0'_SystemOutput.gdx %VSPDInputData%_SystemOutput.gdx';
    put_utility temp 'shell' / 'rename %OutputPath%%runName%\'i_FileName.tl:0'_OfferOutput.gdx %VSPDInputData%_OfferOutput.gdx';
    put_utility temp 'shell' / 'rename %OutputPath%%runName%\'i_FileName.tl:0'_TraderOutput.gdx %VSPDInputData%_TraderOutput.gdx';

    if (%TradePeriodReports% = 1,
       put_utility temp 'shell' / 'rename %OutputPath%%runName%\'i_FileName.tl:0'_BusOutput_TP.gdx %VSPDInputData%_BusOutput_TP.gdx';
       put_utility temp 'shell' / 'rename %OutputPath%%runName%\'i_FileName.tl:0'_BranchOutput_TP.gdx %VSPDInputData%_BranchOutput_TP.gdx';
       put_utility temp 'shell' / 'rename %OutputPath%%runName%\'i_FileName.tl:0'_NodeOutput_TP.gdx %VSPDInputData%_NodeOutput_TP.gdx';
       put_utility temp 'shell' / 'rename %OutputPath%%runName%\'i_FileName.tl:0'_OfferOutput_TP.gdx %VSPDInputData%_OfferOutput_TP.gdx';
       put_utility temp 'shell' / 'rename %OutputPath%%runName%\'i_FileName.tl:0'_BidOutput_TP.gdx %VSPDInputData%_BidOutput_TP.gdx';
       put_utility temp 'shell' / 'rename %OutputPath%%runName%\'i_FileName.tl:0'_ReserveOutput_TP.gdx %VSPDInputData%_ReserveOutput_TP.gdx';
       put_utility temp 'shell' / 'rename %OutputPath%%runName%\'i_FileName.tl:0'_BrConstraintOutput_TP.gdx %VSPDInputData%_BrConstraintOutput_TP.gdx';
       put_utility temp 'shell' / 'rename %OutputPath%%runName%\'i_FileName.tl:0'_MNodeConstraintOutput_TP.gdx %VSPDInputData%_MNodeConstraintOutput_TP.gdx';
    );

*Report the results
    put_utility temp 'exec' / 'gams runVSPDReport';

*Remove the temp files
    put_utility temp 'shell' / 'del %OutputPath%%runName%\%VSPDInputData%_SystemOutput.gdx';
    put_utility temp 'shell' / 'del %OutputPath%%runName%\%VSPDInputData%_OfferOutput.gdx';
    put_utility temp 'shell' / 'del %OutputPath%%runName%\%VSPDInputData%_TraderOutput.gdx';

    if (%TradePeriodReports% = 1,
       put_utility temp 'shell' / 'del %OutputPath%%runName%\%VSPDInputData%_BusOutput_TP.gdx';
       put_utility temp 'shell' / 'del %OutputPath%%runName%\%VSPDInputData%_BranchOutput_TP.gdx';
       put_utility temp 'shell' / 'del %OutputPath%%runName%\%VSPDInputData%_NodeOutput_TP.gdx';
       put_utility temp 'shell' / 'del %OutputPath%%runName%\%VSPDInputData%_OfferOutput_TP.gdx';
       put_utility temp 'shell' / 'del %OutputPath%%runName%\%VSPDInputData%_BidOutput_TP.gdx';
       put_utility temp 'shell' / 'del %OutputPath%%runName%\%VSPDInputData%_ReserveOutput_TP.gdx';
       put_utility temp 'shell' / 'del %OutputPath%%runName%\%VSPDInputData%_BrConstraintOutput_TP.gdx';
       put_utility temp 'shell' / 'del %OutputPath%%runName%\%VSPDInputData%_MNodeConstraintOutput_TP.gdx';
    );
);

*==================================
*Cleanup
*==================================
execute 'del *.ins';
execute 'del *.gdx';

